﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using AutoTAU.Properties;

namespace AutoTAU
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            // Set form closing event handler
            FormClosed += SettingsForm_FormClosed;
            // Set help button
            InitializeComponent();
        }

        /// <summary>
        /// Open the help webpage
        /// </summary>
        private static void OpenHelpPage()
        {
            try
            {
                Process.Start("http://sites.google.com/site/autotauproject/instructions");
            }

            catch (Win32Exception)
            {
                // This happens when there's no default browser. Ignore.
            }
        }

        private void checkBoxAutostart_CheckedChanged(object sender, EventArgs e)
        {
            AutostartManager.Autostart = checkBoxAutostart.Checked;
        }

        private static void SettingsForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            // Save the user's settings
            Settings.Default.Save();

            // When the settings form is closed, attempt to authenticate
            Action authAction = WifiAuthManager.AuthenticateIfConnected;
            authAction.BeginInvoke(authAction.EndInvoke, null);
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenHelpPage();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start("http://sites.google.com/site/autotauproject/request");
            }

            catch (Win32Exception)
            {
                // This happens when there's no default browser. Ignore.
            }
        }
    }
}